package com.yuanfeng.base.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.base.dto.ModularizationFlieDTO;
import com.yuanfeng.base.entity.BaseModularizationFlieEntity;
import com.yuanfeng.base.feign.ThirdPartyServiceClient;
import com.yuanfeng.base.mapper.BaseModularizationFlieMapper;
import com.yuanfeng.base.service.BaseModularizationFlieService;
import com.yuanfeng.base.service.BaseOssConfigService;
import com.yuanfeng.base.vo.ModularizationFlieVO;
import com.yuanfeng.commoms.dto.oos.OssUploadUpdateParamDTO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseMapUtils;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @ClassName BaseModularizationFlieServiceImpl
 * @Description  组件化  图片 视频空间
 * @Author ty
 * @Date 2023-07-04 13:17:22
 * @Version 1.0
 **/
@Service
public class BaseModularizationFlieServiceImpl extends ServiceImpl<BaseModularizationFlieMapper, BaseModularizationFlieEntity> 
        implements BaseModularizationFlieService {

    @Autowired
    private BaseOssConfigService baseOssConfigService;
    @Autowired
    private ThirdPartyServiceClient thirdPartyServiceClient;

    @Override
    public ResponseResult queryYfModularizationFlie(ModularizationFlieDTO param) {
       Map map = null;
       try {
           map = ParseMapUtils.beanToMap(param);
       } catch (IllegalAccessException e) {
           e.printStackTrace();
       }
       IPage<ModularizationFlieVO> page = this.baseMapper.queryYfModularizationFlie(new Query<ModularizationFlieVO>().getPage(map), param);
       return  ResponseResult.success(PageUtils.getPage(page));
    }

    @Override
    public ResponseResult insertVideoUrl(OssUploadUpdateParamDTO ossUploadUpdate) {
        ModularizationFlieVO yfModularizationFlie = new ModularizationFlieVO();
        yfModularizationFlie.setFileName(ossUploadUpdate.getStreamName());
        //获取OSS配置文件中的阿里云API的外网域名
        ResponseResult resultResp = baseOssConfigService.selectGoodsOSSInfo();
        Map<String, Object> map = (Map<String, Object>) resultResp.getData();
        ossUploadUpdate.setEndpoint(map.get("ossFolder").toString());
        ossUploadUpdate.setStreamName(".png");
        ResponseResult resp = thirdPartyServiceClient.uploadToOssBase64(ossUploadUpdate);
        //将数据写入数据库
        yfModularizationFlie.setFileCoverUrl(String.valueOf(resp.getData()));//获取视频封面url
        yfModularizationFlie.setFileUrl(ossUploadUpdate.getFileUrl());//获取视频url
        yfModularizationFlie.setFileType(2);
        Integer integer = this.baseMapper.manageUploadVideo(yfModularizationFlie);
        if(integer > 0){
            return ResponseResult.success();
        }else{
            return ResponseResult.fail();
        }
    }

    @Override
    public ResponseResult uploadImage(ModularizationFlieDTO modularizationFlieDTO) {
        List<OssUploadUpdateParamDTO> paramList = modularizationFlieDTO.getParamList();
        ModularizationFlieVO param = new ModularizationFlieVO();
        //获取OSS配置文件中的阿里云API的外网域名
        ResponseResult resultResp = baseOssConfigService.selectGoodsOSSInfo();
        Map<String, Object> map = (Map<String, Object>) resultResp.getData();
        if (!CollectionUtils.isEmpty(paramList)){
            for (OssUploadUpdateParamDTO file : paramList) {
                String fileName = file.getStreamName(); //文件名加后缀
                param.setFileName(fileName);
                file.setEndpoint(map.get("ossFolder").toString());
                String str1 = fileName.substring(0, fileName.indexOf("."));
                String str2 = fileName.substring(str1.length() + 1);
                file.setStreamName("." + str2);
                ResponseResult resp = thirdPartyServiceClient.uploadToOssBase64(file);
                param.setFileType(1);
                param.setFileUrl((String) resp.getData());
                this.baseMapper.manageUpload(param);
            }
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult deleteYfModularizationFlie(Map<String, Object> param) {
        List<Integer> ids = (List<Integer>) param.get("fileIds");
        if(null != ids && ids.size() > 0 ){
          this.baseMapper.deleteBatchIds(ids);
        }
        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
    }

    @Override
    public ResponseResult queryYfModularizationFlieShop(ModularizationFlieDTO param) {
        UserInfoVO userInfoVO = ParseTokenUtils.getUserInfoByToken(param.getToken());
        if (userInfoVO == null) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);
        }
        param.setShopId(userInfoVO.getShopId());
        //分页
        //将对象转换为map
        Map data = null;
        try {
            data = ParseMapUtils.beanToMap(param);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        //分页查询数据
        IPage<ModularizationFlieVO> page = this.baseMapper.queryYfModularizationFlieShop(new Query<ModularizationFlieVO>().getPage(data),param);
        return ResponseResult.success(PageUtils.getPage(page));
    }

    @Override
    public ResponseResult uploadShopImg(ModularizationFlieDTO param) {
        UserInfoVO userInfoVO = ParseTokenUtils.getUserInfoByToken(param.getToken());
        if (userInfoVO == null){
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);
        }

        List<OssUploadUpdateParamDTO> paramList = param.getParamList();
        ModularizationFlieVO modularizationFlie = new ModularizationFlieVO();
        //获取OSS配置文件中的阿里云API的外网域名
        ResponseResult resultResp = baseOssConfigService.selectGoodsOSSInfo();
        Map<String, Object> map = (Map<String, Object>) resultResp.getData();
        if (!CollectionUtils.isEmpty(paramList)){
            for (OssUploadUpdateParamDTO file : paramList) {
                String fileName = file.getStreamName(); //文件名加后缀
                modularizationFlie.setFileName(fileName);
                file.setEndpoint(map.get("ossFolder").toString());
                String str1 = fileName.substring(0, fileName.indexOf("."));
                String str2 = fileName.substring(str1.length() + 1);
                file.setStreamName("." + str2);
                ResponseResult resp = thirdPartyServiceClient.uploadToOssBase64(file);
                modularizationFlie.setFileType(1);
                modularizationFlie.setFileUrl((String) resp.getData());
                modularizationFlie.setShopId(Integer.valueOf(userInfoVO.getShopId()));
                this.baseMapper.manageUploadShop(modularizationFlie);
            }
        }
        return ResponseResult.success();
    }
}
